﻿function LoadTablesField(obj) {
    if (obj != null) {
        $("[name='AliasCheckBox']:checkbox").removeAttr("checked");
        $(obj).attr("checked", "checked");
    }
    var SaveTablesJson = $("#SelectTablesAndFieldHidden").val();
    var json = eval('(' + SaveTablesJson + ')');
    var tr = ""; var rowIndex = 1;
    for (var i = 0; i < json.length; i++) {
        for (var j = 0; j < json[i].data.length; j++) {
            tr += "<tr>"
                + " <td width=\"20%\" style=\"text-align:center;\">"
                + "     " + rowIndex + ""
                + " </td>"
                + " <td width=\"30%\" style=\"text-align:center;\">"
                + "     " + json[i].tableName + ""
                + " </td>"
                + " <td width=\"20%\" style=\"text-align:center;\">"
                + "     " + json[i].data[j].ColumnName + ""
                + " </td>"
                + " <td width=\"20%\" style=\"text-align:center;\">"
                + "     <input value=\"" + (obj != null && $.trim($(obj).val()) == "取数据库备注列" ? json[i].data[j].ColumnDesc : json[i].data[j].ColumnName) + "\" class=\"inputboder2\"/>"
                + " </td>"
                + " <td width=\"1px\" style=\"text-align:center;display:none;\">"
                + "     <textarea class=\"inputboder2\" style=\"height:50px;\" value=\"" + json[i].data[j].openUrl + "\"></textarea>"
                + " </td>"
                + " <td width=\"20%\" style=\"text-align:center;\">"
                + "     <input type=\"checkbox\" " + (json[i].data[j].isHide=="Y"?"checked=\"checked\"":"") + "/>"
                + " </td>"
                + "</tr>";
            rowIndex++;
        }
    }
    $("#AliasList").html("").append(tr);
}

function BackSetSelectWhere() {
    $("#step6").fadeOut("slow", function () {
        $("#step5").fadeIn("slow");
    });
}

function NextSetOther() {
    var slias = "[";
    $("#AliasList tr").each(function () {
        slias += "{'tableName':'" + $.trim($(this).find("td:eq(1)").text()) + "',"
               + "'fieldName':'" + $.trim($(this).find("td:eq(2)").text()) + "',"
               + "'sliasName':'" + $.trim($(this).find("td:eq(3)").children().val()) + "',"
               + "'openUrl':'" + $.trim($(this).find("td:eq(4)").children().val()) + "',"
               + "'isHide':'" + ($(this).find("td:eq(5)").children().attr("checked") ? "Y" : "N") + "'},";
    });
    slias = slias.substring(0, slias.lastIndexOf(","));
    slias += "]";
    $("#SetSilasHidden").val("").val(slias);
    $("#step6").fadeOut("slow", function () {
        $("#step7").fadeIn("slow", function () {
            LoadTableName();
        });
    });
}

function LoadAliasList() {
    var aliasjson;
    if ($("#SetSilasHidden").val() != "") {
        aliasjson = eval('(' + $("#SetSilasHidden").val() + ')');
    }
    var selectTables;
    if ($("#SelectTablesAndFieldHidden").val() != "") {
        selectTables = eval('(' + $("#SelectTablesAndFieldHidden").val() + ')');
    }
    var tr = "";
    var rowIndex = 1;
    var showAliasName = "";
    var openUrl = ""; 
    var ishide = "";
    for (var i = 0; i < selectTables.length; i++) {
        for (var j = 0; j < selectTables[i].data.length; j++) {
            showAliasName = selectTables[i].data[j].ColumnName;
            if (aliasjson != null) {
                for (var k = 0; k < aliasjson.length; k++) {
                    if (selectTables[i].tableName == aliasjson[k].tableName && selectTables[i].data[j].ColumnName == aliasjson[k].fieldName) {
                        showAliasName = aliasjson[k].sliasName;
                        openUrl = aliasjson[k].openUrl;
                        ishide = aliasjson[k].isHide;
                    }
                }
            }
            tr += "<tr>"
                + " <td width=\"20%\" style=\"text-align:center;\">"
                + "     " + rowIndex + ""
                + " </td>"
                + " <td width=\"30%\" style=\"text-align:center;\">"
                + "     " + selectTables[i].tableName + ""
                + " </td>"
                + " <td width=\"20%\" style=\"text-align:center;\">"
                + "     " + selectTables[i].data[j].ColumnName + ""
                + " </td>"
                + " <td width=\"20%\" style=\"text-align:center;\">"
                + "     <input value=\"" + showAliasName + "\" class=\"inputboder2\"/>"
                + " </td>"
                + " <td width=\"1px\" style=\"text-align:center;display:none;\">"
                + "     <textarea class=\"inputboder2\" style=\"height:50px;width:200px;\" value=\"" + openUrl + "\">" + openUrl + "</textarea>"
                + " </td>"
                + " <td width=\"20%\" style=\"text-align:center;\">"
                + "     <input type=\"checkbox\" " + (ishide == "Y" ? "checked=\"checked\"" : "") + "/>"
                + " </td>"
                + "</tr>";
            rowIndex++;
        }
    }
    $("#AliasList").html("").append(tr);
    $("#TakeDatabaseListing").removeAttr("checked");
}
